
function fake_delete(e)					// dummy function - only for manual testing
{
	$(e.target).css("border","2px solid green");
	alert($(e.target).parents("tr").filter(":first").html());

	$(e.target).parents("tr").filter(":first").css("background-color","red");	
	$(e.target).parents("tr").filter(":first").remove();	
}

function delete_it(e)
{
	// Usage - 
	//	delete_it(eventObject, [resumeTitle], [resumePart])
	// 	eg - delete_it(e,2,skills)	or 		delete_it(e)
	// What does this do ?
	// 	Gets the current resume's title and the part under consideration from the URL (or from the arguements)
	// 	Sends an AJAX post request to delete the delteable object (represented by e)
	// 	If the reponse is positive, removes the DOM table row that contains the deleteable object

	var postId = $(e.target).attr("id");							// id of the item's row to be deleted	
	var pathArray = window.location.pathname.split("/")				// To get the title and part from the URL path (/show/:title/education)
	var title = arguments[1] || pathArray[2];						// second arguement passed (if present) OR third index [2] in the array
	var part = arguments[2] || pathArray[3]								// third arguement passed (if present) OR fourth index [3] in the array
	var elementArray = document.getElementsByName("authenticity_token");	// To get the authenticity token
	var token = elementArray[0].value;										// 	- do -	
	var url = "/destroy/" + title + "/" + part						// URL where the request has to be posted to			

	$.post(															// the AJAX POST
			url,	
			{"id": postId, "authenticity_token": token},			// The id and token being sent as data
			function(response){	delete_callback(e,response); }
	);
}

function delete_callback(e, response)
{
	// What does this do ? 
	// Removes the table row of which the deleted object is a part
	// Traverses throught the deleted element's DOM parents and deletes teh first element with a TR tag
	
	var responseObj = JSON.parse(response);
	var parentRow, moreInfoRow;
	
	if (responseObj["retVal"] == "deleted"	){
		parentRow = $(e.target).parents("tr").filter(":first");
		moreInfoRow = parentRow.next(".more_info_row").eq(0);		// more_info_row should be the next sibling
		parentRow.remove();
		if (moreInfoRow){								// If there is a more_info row separate, delete it too
			moreInfoRow.remove();
		}	
	}
	else{
		alert(responseObj["retVal"]);				// XXX: Change this action to something better
	}
}

function display_with_linebreaks(element)
{	
	// display the element with line breaks at \n
	var temp = $(element).html().replace(/\n/g,'<br>');	// /\n/ is the regex, g (global) is to replace all occurances
	$(element).html(temp);	
}

function splitName(nameObj)
{
	// splits string on square brackets. eg -
	// splits 'education[23][level]' into an array {'education', '23', 'level'}
	// or skills[skillset_type]' to array {'skills','skillset_type'}
	var str = nameObj;
	var finalarr = new Array();
	var arr1 = 	str.split('[');
	finalarr.push(arr1[0]);
	for (i=1; i<arr1.length; i++){
		arr2 = arr1[i].split(']');
		finalarr.push(arr2[0]);
	}
	return finalarr;	
}